//
//  AddFamiliarityNumberViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/4.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "AddFamiliarityNumberViewController.h"
#import "AddAffectionNumberApi.h"

@interface AddFamiliarityNumberViewController ()

/** 联系人 **/
@property(nonatomic,strong) UITextField *nameTF;
@property(nonatomic,strong) UITextField *phoneTF;
@property(nonatomic,strong) UIButton *addButton;


@end

@implementation AddFamiliarityNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark --- 点击事件
- (void)didClickAddButton
{
    NSString *nickName = [self.nameTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (nickName == 0) {
        [SVProgressHUD showInfoWithStatus:@"名字不能为空"];
        return;
    }
    if (nickName.length > 10) {
        [SVProgressHUD showInfoWithStatus:@"名字长度不能超过10个字符"];
        return;
    }
    
    if ([NSString isContainsTwoEmoji:nickName] ) {
        [SVProgressHUD showInfoWithStatus:@"名字不能使用表情"];
        return;
    }
    
    if (![self.phoneTF.text isMobileNumber])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    
    
    AddAffectionNumberApi *api = [[AddAffectionNumberApi alloc] initWithDic:@{@"sn":EXRACT_OBJECT(DEVICEMAC),@"fullname":nickName,@"mobile":self.phoneTF.text}];
    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showSuccessWithStatus:[request responseJSONObject][@"errmsg"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:nil];
    

}

#pragma mark -- 懒加载
- (UITextField *)nameTF
{
    if(!_nameTF)
    {
        UIView *superView = self.view;
        _nameTF = [[UITextField alloc]init];
        [superView addSubview:_nameTF];
        _nameTF.placeholder = @"请输入联系人名称";
        _nameTF.textColor = [UIColor whiteColor];
        [_nameTF setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];

    }
    return _nameTF;
}
- (UITextField *)phoneTF
{
    if(!_phoneTF)
    {
        UIView *superView = self.view;
        _phoneTF = [[UITextField alloc]init];
        [superView addSubview:_phoneTF];
        _phoneTF.placeholder = @"请输入联系人号码";
        _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTF.textColor = [UIColor whiteColor];
        [_phoneTF setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];

    }
    return _phoneTF;
}

- (UIButton *)addButton
{
    if(!_addButton)
    {
        UIView *superView = self.view;
        _addButton = [[UIButton alloc]init];
        [superView addSubview:_addButton];
        [_addButton setTitle:@"添加" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_addButton.titleLabel setFont:fontCommonSize];
        [_addButton setBackgroundColor:rgba(0,159,236,1)];
        _addButton.layer.cornerRadius = 5;
        [_addButton addTarget:self action:@selector(didClickAddButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _addButton;
}


- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    NSArray *array = @[self.nameTF,self.phoneTF];
    NSArray *titleArray = @[@"联系人",@"电话号码"];
    UIView *lastView;
    [superView addSubview:lastView];
    for (int i = 0; i < titleArray.count; i++)
    {
        UILabel *nameLabel = ({
            nameLabel = [[UILabel alloc] init];
            [superView addSubview:nameLabel];
            nameLabel.text = titleArray[i];
            nameLabel.font = fontWithSize(16);
            nameLabel.textColor = [UIColor whiteColor];
            [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                if (lastView)
                {
                    make.top.mas_equalTo(lastView.mas_top).with.offset(px1920Height(75));
                }
                else
                {
                    make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(271));
                }
                make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
                make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
            }];
            nameLabel;
        });

        UITextField *textField = array[i];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(nameLabel.mas_bottom).with.offset(px1920Height(52));
            make.left.right.mas_equalTo(nameLabel);
        }];
        
        UIView *lineView = ({
            lineView = [[UIView alloc]init];
            [superView addSubview:lineView];
            lineView.backgroundColor = rgba(27,44,81,1);
            [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(textField.mas_bottom).with.offset(px1920Height(30));
                make.left.right.mas_equalTo(nameLabel);
                make.height.mas_equalTo(1);
            }];
            lineView;
        });
        lastView = lineView;
    }
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lastView.mas_bottom).with.offset(px1920Height(150));
        make.left.right.mas_equalTo(lastView);
        make.height.mas_equalTo(px1920Height(136));
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"亲情号码添加"];
}

@end
